﻿using Store.Manager.ERP.Application.Dtos;
using Store.Manager.ERP.Domain.Entities;
using Store.Manager.ERP.Domain.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Store.Manager.ERP.Application.Services
{
    public class ProductService : IProductService
    {
        private readonly IProductRepository _productRepository;

        public ProductService(IProductRepository productRepository)
        {
            _productRepository = productRepository;
        }

        public async Task<ProductDto> AddProductAsync(ProductDto productDto)
        {
            var product = new Product
            {
                Code = productDto.Code,
                Name = productDto.Name,
                Category = productDto.Category,
                Size = productDto.Size,
                Color = productDto.Color,
                Price = productDto.Price,
                Stock = productDto.Stock,
                Season = productDto.Season,
                Fabric = productDto.Fabric,
                Brand = productDto.Brand,
                Description = productDto.Description
            };
            await _productRepository.AddAsync(product);

            return new ProductDto
            {
                Code = product.Code,
                Name = product.Name,
                Category = product.Category,
                Size = product.Size,
                Color = product.Color,
                Price = product.Price,
                Stock = product.Stock,
                Season = product.Season,
                Fabric = product.Fabric,
                Brand = product.Brand,
                Description = product.Description
            };
        }

        public async Task<List<ProductDto>> AddProductsAsync(List<ProductDto> productDtos)
        {
            var products = productDtos.Select(dto => new Product
            {
                Code = dto.Code,
                Name = dto.Name,
                Category = dto.Category,
                Size = dto.Size,
                Color = dto.Color,
                Price = dto.Price,
                Stock = dto.Stock,
                Season = dto.Season,
                Fabric = dto.Fabric,
                Brand = dto.Brand,
                Description = dto.Description
            }).ToList();

            await _productRepository.AddRangeAsync(products);

            return products.Select(p => new ProductDto
            {
                Code = p.Code,
                Name = p.Name,
                Category = p.Category,
                Size = p.Size,
                Color = p.Color,
                Price = p.Price,
                Stock = p.Stock,
                Season = p.Season,
                Fabric = p.Fabric,
                Brand = p.Brand,
                Description = p.Description
            }).ToList();
        }

        public async Task UpdateProductAsync(ProductDto productDto)
        {
            var product = await _productRepository.GetByCodeAsync(productDto.Code);
            if (product != null)
            {
                product.Name = productDto.Name;
                product.Category = productDto.Category;
                product.Size = productDto.Size;
                product.Color = productDto.Color;
                product.Price = productDto.Price;
                product.Stock = productDto.Stock;
                product.Season = productDto.Season;
                product.Fabric = productDto.Fabric;
                product.Brand = productDto.Brand;
                product.Description = productDto.Description;
                await _productRepository.UpdateAsync(product);
            }
        }

        public async Task DeleteProductAsync(string code)
        {
            await _productRepository.DeleteAsync(code);
        }

        public async Task<ProductDto> GetProductByCodeAsync(string code)
        {
            var product = await _productRepository.GetByCodeAsync(code);
            if (product != null)
            {
                return new ProductDto
                {
                    Code = product.Code,
                    Name = product.Name,
                    Category = product.Category,
                    Size = product.Size,
                    Color = product.Color,
                    Price = product.Price,
                    Stock = product.Stock,
                    Season = product.Season,
                    Fabric = product.Fabric,
                    Brand = product.Brand,
                    Description = product.Description
                };
            }
            return null;
        }

        public async Task<List<ProductDto>> GetAllProductsAsync()
        {
            var products = await _productRepository.GetAllAsync();
            return products.Select(p => new ProductDto
            {
                Code = p.Code,
                Name = p.Name,
                Category = p.Category,
                Size = p.Size,
                Color = p.Color,
                Price = p.Price,
                Stock = p.Stock,
                Season = p.Season,
                Fabric = p.Fabric,
                Brand = p.Brand,
                Description = p.Description
            }).ToList();
        }
    }
}
